package com.example.viewdraghelper.demo;

import java.util.Random;

import com.example.qq7.R;
import com.example.viewdraghelper.view.DragLayout;
import com.example.viewdraghelper.view.DragLayout.OnDragLayoutStateChangedListener;
import com.nineoldandroids.animation.FloatEvaluator;
import com.nineoldandroids.view.ViewHelper;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	/**DragLayout控件*/
	private DragLayout mDragLayout;
	/**中文姓名的ListView即主面板*/
	private ListView lv_main;
	/**英文姓名的ListView即菜单面板*/
	private ListView lv_menu;
	/**主面板头部的头像*/
	private ImageView iv_head;
	/**Float类型的估值器*/
	private FloatEvaluator fe;
	/**主面板右上角的控制菜单面板打开关闭的图片按钮*/
	private ImageView iv_open;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		
		initView();
		initListener();
	}
	
	private void initView(){
		mDragLayout = (DragLayout) findViewById(R.id.dragLayout);
		lv_main = (ListView) findViewById(R.id.lv_names_cn);
		lv_menu = (ListView) findViewById(R.id.lv_names_en);
		ArrayAdapter<String> mainAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, Cheeses.NAMES);
		ArrayAdapter<String> menuAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, Cheeses.sCheeseStrings){
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				TextView tv = (TextView) super.getView(position, convertView, parent);
				tv.setTextColor(Color.WHITE);
				return tv;
			}
		};
		lv_main.setAdapter(mainAdapter);
		lv_menu.setAdapter(menuAdapter);
		
		iv_head = (ImageView) findViewById(R.id.iv_head);
		iv_open = (ImageView) findViewById(R.id.iv_open);
	}
	
	
	private void initListener(){
		fe = new FloatEvaluator();
		mDragLayout.setOnDragLayoutStateChangedListener(new MyOnDragLayoutStateChangedListener());
		iv_open.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mDragLayout.open();
			}
		});
	}

	private class MyOnDragLayoutStateChangedListener implements OnDragLayoutStateChangedListener{
		//当发生回调事件时需要做哪些事情.
		//1.菜单面板完全打开时,lv_menu随机滚动;
		//2.菜单面板完全关闭时,主面板头像抖动;
		//3.拖拽时背景从暗变量,主面板头像变为透明.
		
		@Override
		public void onOpen() {
			int position = new Random().nextInt(Cheeses.sCheeseStrings.length);
			lv_menu.smoothScrollToPosition(position);
		}

		@Override
		public void onClose() {
			//位移动画,从中间到右边的晃动.
			TranslateAnimation ta = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0.5f, 
					Animation.RELATIVE_TO_SELF, 1.0f, 
					Animation.RELATIVE_TO_SELF, 0, 
					Animation.RELATIVE_TO_SELF, 0);
			//设置插值器,让它晃动3个周期.
			ta.setInterpolator(new CycleInterpolator(3));
			ta.setDuration(500);
			iv_head.startAnimation(ta);
		}

		@Override
		public void onDraging(float percent) {
			//透明度动画.
			Float evaluate = fe.evaluate(percent, 1.0f, 0.3f);
			ViewHelper.setAlpha(iv_head, evaluate);
		
			//setColorFilter的原理给drawable蒙上一层图片(图片从黑色变为透明),模式为SRC_OVER.
			Drawable bg = mDragLayout.getBackground();
			bg.setColorFilter(
					evaluateArgb(percent, Color.BLACK, Color.TRANSPARENT),
					PorterDuff.Mode.SRC_OVER);
		}
	}

	// ArgbEvaluator不同版本实现不同,早期版本忽略了透明度通道.
	//下面是最新版的ArgbEvaluator的实现方法,这里直接拷过来使用.
	public int evaluateArgb(float fraction, Object startValue, Object endValue) {
		int startInt = (Integer) startValue;
		int startA = (startInt >> 24) & 0xff;
		int startR = (startInt >> 16) & 0xff;
		int startG = (startInt >> 8) & 0xff;
		int startB = startInt & 0xff;

		int endInt = (Integer) endValue;
		int endA = (endInt >> 24) & 0xff;
		int endR = (endInt >> 16) & 0xff;
		int endG = (endInt >> 8) & 0xff;
		int endB = endInt & 0xff;

		return (int) ((startA + (int) (fraction * (endA - startA))) << 24)
				| (int) ((startR + (int) (fraction * (endR - startR))) << 16)
				| (int) ((startG + (int) (fraction * (endG - startG))) << 8)
				| (int) ((startB + (int) (fraction * (endB - startB))));
	}
}
